package com.shanghaionstar.sqlite;

import android.content.Context;

import com.onstar.cn.sqlite.entities.OvdHistory;
import com.onstar.cn.sqlite.xutil.db.sqlite.Selector;
import com.onstar.cn.sqlite.xutil.db.sqlite.WhereBuilder;
import com.onstar.cn.sqlite.xutil.exception.DbException;


import java.util.ArrayList;
import java.util.List;

/**
 * @author Xiaofeng Feng
 * @ClassName: DatabaseOvdHistory
 * @Description: 车况数据存储
 * @date 2015年8月14日 下午2:15:18
 */
public class DatabaseOvdHistory extends BaseSqlImp {

    public DatabaseOvdHistory(Context context) {
        super(context);
    }

    /**
     * (non-Javadoc)
     * <p>Title: insert</p>
     * <p>Description: </p>
     *
     * @param p_Info
     * @return
     * @see
     */
    public long insert(OvdHistory p_Info) {
        long rowId = 0;
        try {
            mDbUtils.saveBindingId(p_Info);
            rowId = mDbUtils.count(OvdHistory.class);
        } catch (Exception e) {
            e.printStackTrace();
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }
        return rowId;
    }

    /**
     * (non-Javadoc)
     * <p>Title: update</p>
     * <p>Description: </p>
     *
     * @param p_Info
     * @return
     * @see
     */
    public void update(OvdHistory p_Info) {
        try {
            mDbUtils.update(p_Info, WhereBuilder.b("vin", "=", p_Info.getVin()));
        } catch (DbException e) {
            e.printStackTrace();
        }
//		return super.update(p_Info, "vin=?", new String[] { p_Info.getVin() });
    }

    /**
     * @param @param id    设定文件
     * @return void    返回类型
     * @throws
     * @Title: delete
     * @Description:
     */
    public void delete(String id) {
        try {
            mDbUtils.deleteById(OvdHistory.class, id);
        } catch (DbException e) {
            e.printStackTrace();
        }
//		super.delete("_id=? ", new String[] { id });
    }

    /**
     * @param @param  info
     * @param @return 设定文件
     * @return boolean    返回类型
     * @throws
     * @Title: checkDataIfExits
     * @Description: info
     */
    public boolean checkDataIfExits(OvdHistory info) {
        return checkDataIfExits(info.getVin());
    }

    /**
     * @param @param  vin
     * @param @return 设定文件
     * @return boolean    返回类型
     * @throws
     * @Title: checkDataIfExits
     * @Description: info
     */
    public boolean checkDataIfExits(String vin) {
        boolean result = false;
        List<OvdHistory> list = new ArrayList<OvdHistory>();
        try {
//			list = getAll(null, "vin=? ", new String[] { vin }, null, null, null, null);
            list = mDbUtils.findAll(Selector.from(OvdHistory.class).where("vin", "=", vin));
            if (list.size() > 0) {
                if (!list.get(0).getVin().equals("")) {
                    result = true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @param @param  vin
     * @param @param  isRead
     * @param @return 设定文件
     * @return List<OvdHistory>    返回类型
     * @throws
     * @Title: getAllOvdHistory
     * @Description: 查询所有未读或已读消息isRead='true' or isRead='false'
     */
    public List<OvdHistory> getAllOvdHistory(String vin, String isRead) {
        List<OvdHistory> list = new ArrayList<OvdHistory>();
        try {
            String orderBy = null;
            orderBy = OvdHistory.ISREAD + " desc ";
//			list = getAll(null, "vin=? and isRead=? ", new String[] { vin , isRead}, null, null, orderBy, null);
            list = mDbUtils.findAll(Selector.from(OvdHistory.class).where("vin", "=", vin).and("isRead", "=", isRead));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * @param @param  vin
     * @param @return 设定文件
     * @return OvdHistory    返回类型
     * @throws
     * @Title: getOvdHistory
     * @Description: 根据vin号查询单个历史
     */
    public OvdHistory getOvdHistory(String vin) {
        List<OvdHistory> list = new ArrayList<OvdHistory>();
        try {
//			String orderBy = null;
//				orderBy = OvdHistory.CREATEDATE + " desc ";
//			list = getAll(null, "vin=? ", new String[] { vin }, null, null, orderBy, null);
            list = mDbUtils.findAll(Selector.from(OvdHistory.class).where("vin", "=", vin).orderBy(OvdHistory.CREATEDATE, true));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    /**
     * @param @param  vin
     * @param @return 设定文件
     * @return List<OvdHistory>    返回类型
     * @throws
     * @Title: getAllOvdHistory
     * @Description: 查询所有消息
     */
    public List<OvdHistory> getAllOvdHistory(String vin) {
        List<OvdHistory> list = new ArrayList<OvdHistory>();
        try {
//			String orderBy = null;
//				orderBy = OvdHistory.ISREAD + " desc ";
//			list = getAll(null, "vin=? ", new String[] { vin }, null, null, orderBy, null);
            list = mDbUtils.findAll(Selector.from(OvdHistory.class).where("vin", "=", vin).orderBy(OvdHistory.ISREAD, true));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
